#pragma once

#include <algorithm>
#include <vector>

namespace solution_1024
{

class Solution
{
public:
    int videoStitching(const std::vector<std::vector<int>>& clips, int T)
    {
        std::vector<int> right_most(T, 0);
        for (auto& clip : clips) {
            auto l = clip.at(0) < 0 ? 0 : clip.at(0);
            auto r = clip.at(1);
            if (r >= 0 && l < T)
                right_most.at(l) = std::max(right_most.at(l), r);
        }
        int num = 0;
        for (int i = 0, left = 0, rmost = 0; i < T; ++i) {
            rmost = std::max(right_most.at(i), rmost);
            if (rmost == i)
                return -1;
            if (i == left) {
                left = rmost;
                ++num;
            }
        }
        return num;
    }
};

}  /* namespace solution_1024 */
